﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockBusiness;
using MockDataTransferObject;
using MockCommon;
using System.Collections.ObjectModel;
using System.Globalization;

namespace Presentation
{
    public partial class Resolve : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                MessageBusiness messageBusiness = MessageBusiness.RetrieveInstance();
                if (Session["user"] != null && Session["user"] is User)
                {
                    User user = (User)Session["user"];
                    if (user != null)
                    {
                        if (Request.QueryString["action"] != null)
                        {
                            if (Request.QueryString["action"].ToString().Equals("received"))
                            {
                                messageBusiness.UpdateReceiveAll(user.UserId);
                            }
                        }
                        else
                        {
                            string newest = "";
                            int unreceicedMessageCount = messageBusiness.GetCountMessage(user.UserId, false);
                            string rowMessage = "";
                            Collection<Message> messages = messageBusiness.GetTopMessage(user.UserId, 5);
                            int i = 0;
                            foreach (Message message in messages)
                            {
                                if (i == 0)
                                {
                                    newest = message.SendDate.ToString("ddMMyyyyhhmmssfff", CultureInfo.CurrentCulture);
                                }
                                double range = (DateTime.Now - message.SendDate).TotalSeconds;
                                rowMessage += String.Format(CultureInfo.CurrentCulture, "{0}\b{1}\b{2}\n", range, message.MessageId, message.Title);
                                i++;
                            }
                            string responseText = String.Format(CultureInfo.CurrentCulture, "{0}\n{1}\n{2}", unreceicedMessageCount, newest, rowMessage);
                            Response.Write(responseText);
                            Response.End();
                        }
                    }
                }
                else
                {
                    Response.Write("");
                    Response.End();
                }
            }
            catch (MockException)
            {
                Response.Write("");
                Response.End();
            }
        }
    }
}